package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.SysDepart;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysDepartService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/sys/depart")
public class SysDepartController {

    @Resource(name = "sysDepartService")
    private SysDepartService sysDepartService;

    @GetMapping
    public Result<List<SysDepart>> getList(String name) throws IframeException {
        LambdaQueryWrapper<SysDepart> lambdaQueryWrapper = new LambdaQueryWrapper<SysDepart>();
        if(StringUtils.isNotEmpty(name)){
            lambdaQueryWrapper.like(SysDepart::getName , name);
        }
        return Result.ok(sysDepartService.selectList(lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<SysDepart> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysDepartService.getById(id));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody SysDepart sysDepart) throws IframeException {
        return Result.ok(sysDepartService.save(sysDepart));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody SysDepart sysDepart) throws IframeException {
        return Result.ok(sysDepartService.update(sysDepart));
    }
    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysDepartService.delete(id));
    }
    @GetMapping("/status")
    public Result<Boolean> status(@RequestParam("id") Integer id, @RequestParam("status") Integer status) throws IframeException {
        SysDepart sysDepart = new SysDepart();
        sysDepart.setId(id);
        sysDepart.setStatus(status);
        return Result.ok(sysDepartService.updateById(sysDepart));
    }

    @GetMapping("/list")
    public Result<List<SysDepart>> getList() throws IframeException{
        LambdaQueryWrapper<SysDepart> lambdaQueryWrapper = new LambdaQueryWrapper<SysDepart>();
        lambdaQueryWrapper.eq(SysDepart::getStatus , 0);
        return Result.ok(sysDepartService.selectList());
    }



}
